﻿using LuShanTea.DTO;
using LuShanTea.Front.Web.Models;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Front.Web.App_Start
{
    public class UserDataHelper
    {
        public IGoodService GoodService = DependencyResolver.Current.GetService<IGoodService>();

        public IGoodImsSrcService GoodImsSrcService = DependencyResolver.Current.GetService<IGoodImsSrcService>();

        public IOrderService OrderService = DependencyResolver.Current.GetService<IOrderService>();

        public IOrderRecordService OrderRecordService = DependencyResolver.Current.GetService<IOrderRecordService>();

        public ICollectService CollectService = DependencyResolver.Current.GetService<ICollectService>();
        /// <summary>
        /// 用户收藏的商品
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public UserCollectModel[] GetUserCollectModel(long userid)
        {
            #region 我的收藏
            var collects = CollectService.GetCollectsByUserId(userid, 1, 5);

            //获取收藏的id
            long[] collectIds = collects.Select(c => c.Id).ToArray();

            //获取所有收藏商品的id
            long[] goodIds = collects.Select(c => c.GoodId).ToArray();

            List<UserCollectModel> listUserCollectModels = new List<UserCollectModel>();

            int index = 0;

            //遍历我收藏的商品Id
            foreach (long goodid in goodIds)
            {

                var good = GoodService.GetByGoodId(goodid);

                //获取商品第一张图片的地址
                GoodImgSrcDTO goodImgSrcDTO = GoodImsSrcService.GetFirstImgByGoodId(goodid);

                string imgSrc = goodImgSrcDTO == null ? "" : goodImgSrcDTO.ImgSrc;

                listUserCollectModels.Add(new UserCollectModel { Good = good, GoodImgSrc = imgSrc, CollectId = collectIds[index++] });
            }

            return listUserCollectModels.ToArray();
            #endregion
        }

        public UserOrderRecordModel[] GetOrderRecords(long userId, HttpContextBase httpContext, ViewDataDictionary viewData)
        {
            long userid = UserHelper.GetUserId(httpContext);

            int count = 0;

            #region 获取用户30天的订单数据

            var orders = OrderService.GetUserOrderToDay(userid, 30);

            if (orders == null)
            {
                viewData["OrderRecordCount"] = 0;
                return new UserOrderRecordModel[] { };
            }


            //long[] orderIds = orders.Select(o => o.Id).ToArray();


            var orderRecords = GetUserOrderRecor(orders, out count);

            viewData["OrderRecordCount"] = count;

            return orderRecords;
            #endregion
        }

        /// <summary>
        /// 获取商品订单的商品记录，以及商品图片
        /// </summary>
        /// <param name="orders"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public UserOrderRecordModel[] GetUserOrderRecor(OrderDTO[] orders, out int count)
        {
            count = 0;
            List<UserOrderRecordModel> listModel = new List<UserOrderRecordModel>();

            //遍历我的订单
            foreach (var order in orders)
            {
                //获取订单下的订单记录来
                OrderRecordDTO[] orderRecordDTOs = OrderRecordService.GetOrderRecordByOrderId(order.Id);

                List<string> listImgSrcs = new List<string>();

                foreach (var orderRe in orderRecordDTOs)
                {
                    count++;
                    //获取订单记录里的商品图片
                    GoodImgSrcDTO goodImgSrcDTO = GoodImsSrcService.GetFirstImgByGoodId(orderRe.GoodId);

                    string imgSrc = goodImgSrcDTO == null ? "" : goodImgSrcDTO.ImgSrc;

                    listImgSrcs.Add(imgSrc);
                }

                listModel.Add(new UserOrderRecordModel { Order = order, OrderRecord = orderRecordDTOs, ImgSrcs = listImgSrcs.ToArray() });
            }

            return listModel.ToArray();
        }
    }
}